import {
    _get,
    _post
} from '../../utils/request'
import util from '../../utils/util'
const app = getApp()
Page({
    data: {
        code: '',
        password: '',
        checkedValue: '',
        switchChecked: true,
        testList: [
            [
                { res: { name: 'fbf', age: 19 }, mate: '啊啊' },
                { res: { name: 'aaa', age: 20 }, mate: '四晒' }
            ],
            [
                { res: { name: 'bbb', age: 11 }, mate: '传参' },
                { res: { name: 'ddd', age: 15 }, mate: '传参' },
                { res: { name: 'ccc', age: 22 }, mate: '传参' },
            ]
        ]
    },
    //判断是否要记住密码
    bindswitchchange(event) {

        this.setData({
            switchChecked: event.detail.value,
        })

    },
    //登录缓存密码
    LoginForm: function(e) { //登录加载效果

        let checkedValue = this.data.switchChecked;
        console.log(checkedValue);
        //如果记住密码则向微信缓存写入账号密码
        //如果不记住密码则清空微信缓存存在的账号密码
        if (checkedValue == true) {
            // dd.setStorageSync("code", this.data.code);
            // dd.setStorageSync("password", this.data.password);
            // dd.setStorageSync("checkedValue", this.data.switchChecked);
            dd.setStorageSync({
                key: 'login',
                data: {
                    code: this.data.code,
                    password: this.data.password,

                }
            });
        } else if (checkedValue == false) {
            dd.setStorageSync({
                key: 'login',
                data: {
                    code: '',
                    password: '',

                }
            });
        }
    },
    write(options) {
        // console.log(dd.getStorageSync({ key: 'login' }));
        this.setData({
            code: dd.getStorageSync({ key: 'login' }).data.code,
            password: dd.getStorageSync({ key: 'login' }).data.password,

        })
    },
    oninput(e) {
        // console.log('input', e);
        this.setData({
            [e.target.dataset.key]: e.detail.value
        })
    },
    login() {

        let params = {
                password: this.data.password,
                username: this.data.code
            }
            // console.log(params);
        _get(`/user/login`, params).then(res => {
            console.log('login', res);
            this.LoginForm()
            app.userId = res.data
            if (res.code == 200) {
                dd.alert({ content: '登录成功' });
                dd.reLaunch({
                    url: '/pages/home/home'
                })
            } else {
                dd.alert({
                    title: '账号或密码错误',
                });

            }
        }).catch(err => {
            dd.alert({ content: err })
        })
    },

    onLoad() {
        this.write();
        console.log(this.data.testList.flat());
    },
});